package com.ruoyi.web.controller.net;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.net.domain.*;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.IBusiMtnGroupPortService;
import com.ruoyi.net.service.IBusiMtnGroupService;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 业务-mtnGroup配置Controller
 * 
 * @author net
 * @date 2024-12-07
 */
@Api(tags = "业务-mtnGroup配置")
@RestController
@RequestMapping("/busi/mtn/group")
public class BusiMtnGroupController extends BaseController {
    @Autowired
    private IBusiMtnGroupService busiMtnGroupService;
    @Autowired
    private IBusiMtnGroupPortService busiMtnGroupPortService;
    @Autowired
    private DeviceManager deviceManager;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private INetElementService netElementService;

    /**
     * 查询业务-mtnGroup配置列表
     */
    @ApiOperation(value = "查询业务-mtnGroup配置列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusiMtnGroup busiMtnGroup) {
        startPage();
        List<BusiMtnGroupListVo> list = busiMtnGroupService.selectBusiMtnGroupList(busiMtnGroup);
        return getDataTable(list);
    }

    /**
     * 查询业务-mtnGroup配置列表
     */
    @ApiOperation(value = "查询网元-业务-mtnGroup列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:group:list')")
    @GetMapping("/ele/list")
    public TableDataInfo eleList(BusiMtnGroupListVo busiMtnGroup) {
        startPage();
        List<BusiMtnGroupListVo> list = busiMtnGroupService.selectBusiMtnGroupEleList(busiMtnGroup);
        return getDataTable(list);
    }

    /**
     * 导出业务-mtnGroup配置列表
     */
    @ApiOperation(value = "导出业务-mtnGroup配置列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:group:export')")
    @Log(title = "业务-mtnGroup配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusiMtnGroup busiMtnGroup) {
        List<BusiMtnGroupListVo> list = busiMtnGroupService.selectBusiMtnGroupList(busiMtnGroup);
        ExcelUtil<BusiMtnGroupListVo> util = new ExcelUtil<BusiMtnGroupListVo>(BusiMtnGroupListVo.class);
        util.exportExcel(response, list, "业务-mtnGroup配置数据");
    }

    /**
     * 获取业务-mtnGroup配置详细信息
     */
    @ApiOperation(value = "获取业务-mtnGroup配置详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(busiMtnGroupService.selectBusiMtnGroupById(id));
    }

    /**
     * 新增业务-mtnGroup配置
     */
    @ApiOperation(value = "新增业务-mtnGroup配置", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:group:add')")
    @Log(title = "业务-mtnGroup配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiMtnGroupParamVo busiMtnGroupParamVo) {
        return toAjax(busiMtnGroupService.insertBusiMtnGroup(busiMtnGroupParamVo));
    }

    /**
     * 修改业务-mtnGroup配置
     */
    @ApiOperation(value = "修改业务-mtnGroup配置", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('net:group:edit')")
    @Log(title = "业务-mtnGroup配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiMtnGroup busiMtnGroup) {
        return toAjax(busiMtnGroupService.updateBusiMtnGroup(busiMtnGroup));
    }

    /**
     * 删除业务-mtnGroup配置
     */
    @ApiOperation(value = "删除业务-mtnGroup配置", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('net:group:remove')")
    @Log(title = "业务-mtnGroup配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(busiMtnGroupService.deleteBusiMtnGroupByIds(ids));
    }

    /**
     * 查询业务-mtnGroup配置列表
     */
    @ApiOperation(value = "计算Phy-num配置列表", httpMethod = "GET")
    @GetMapping("/caculatePhyList")
    public TableDataInfo caculatePhyList(BusiMtnGroup busiMtnGroup) {
        startPage();
        List<NetElementLinkVo> list = busiMtnGroupService.caculatePhyList(busiMtnGroup);
        return getDataTable(list);
    }

    /**
     * 激活设备
     *
     * @param id
     */
    @ApiOperation(value = "激活设备", httpMethod = "GET")
    @GetMapping(value = "/activate/{id}")
    @Log(title = "激活设备", businessType = BusinessType.UPDATE)
    public AjaxResult activateDevice(@PathVariable("id") String id) {
        BusiMtnGroup entity = busiMtnGroupService.selectBusiMtnGroupById(id);
        entity.setStatus("1");
        if (sendXml(entity)) {
            return toAjax(busiMtnGroupService.updateBusiMtnGroup(entity));
        } else {
            return AjaxResult.error("激活设备失败");
        }
    }

    /**
     * 去激活设备
     *
     * @param id
     */
    @ApiOperation(value = "去激活设备", httpMethod = "GET")
    @Log(title = "去激活设备", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/unactivate/{id}")
    public AjaxResult unactivateDevice(@PathVariable("id") String id) {
        BusiMtnGroup entity = busiMtnGroupService.selectBusiMtnGroupById(id);
        entity.setStatus("0");
        if (sendXml(entity)) {
            return toAjax(busiMtnGroupService.updateBusiMtnGroup(entity));
        } else {
            return AjaxResult.error("激活设备失败");
        }
    }
     /*
       <if xmlns="http://fiberhome.com/fhnw/yang/interface/fos-interface-common">
    <interfaces>
      <interface">
        <name>flexe-group1</name>
        <type>FLEXE-GROUP</type>
     <flexegrp-ctrlinterface xmlns="http://fiberhome.com/fhnw/yang/interface/flexeinterface/fos-flexeif-group">
          <work-mode>static</work-mode>
          <group-num>1</group-num>
          <fccas-enable>disable</fccas-enable>
          <slot-granularity>5g</slot-granularity>
          <flexe-group-member>
            <flexe-name>flexe-100gi0/6/0/1</flexe-name>
            <phy-num>1</phy-num>???
          </flexe-group-member>
          <cal-negotiation-overtime>1</cal-negotiation-overtime>
        </flexegrp-ctrlinterface>
              </interface>
    </interfaces>
  </if>
        */

    /**
     * 发送xml到设备
     *
     * @param entity
     */
    private boolean sendXml(BusiMtnGroup entity) {
        // TODO: 2024/11/22 待与设备联调
        boolean isSend = true;
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getStartElement());
//            busiMtnGroupPortService.selectBusiMtnGroupPortList()
            deviceManager.getDevice(netElement.getIp());
            String xml = "<if xmlns=\"http://fiberhome.com/fhnw/yang/interface/fos-interface-common\">" +
                    "<interfaces>" +
                    "<interface>" +
                    "<name>" + entity.getBusiName() + "</name>" +
                    "<type>FLEXE-GROUP</type>" +
                    "<flexegrp-ctrlinterface xmlns=\"http://fiberhome.com/fhnw/yang/interface/flexeinterface/fos-flexeif-group\">" +
                    "   <work-mode>" + entity.getWorkMode() +
                    "   </work-mode>" +
                    "   <group-num>" + entity.getGroupNum() +
                    "   </group-num>" +
                    "   <fccas-enable>" + entity.getReduceSpeedEnable() +
                    "   </fccas-enable>" +
                    "   <slot-granularity>" + entity.getFaultSize() + "</slot-granularity>" +
                    "   <cal-negotiation-overtime>" + entity.getOverTime() +
                    "   </cal-negotiation-overtime>" +
                    " </flexegrp-ctrlinterface>" +
                    "</interface>" +
                    "</interfaces>"
                    + "</if>";
            System.out.println(xml);
            isSend = deviceManager.editConfig(xml, "merge");
        }
        return isSend;
    }
}